// 节流函数
export function throttle(fn, delay = 1000, ) {
	let lastTime = 0
	let timer = null
	
	function _throttle(...args) {
		return new Promise((resolve) => {
			let nowTime = new Date().getTime()
			let remainTime = delay - (nowTime - lastTime)
			if(remainTime <= 0) {
				if(timer) {
					clearTimeout(timer)
					timer = null
				}
				const result = fn.apply(this, args)
				resolve(result)
				lastTime = nowTime
				return
			}
			if(!timer) {
				timer = setTimeout(() => {
					timer = null
					const result = fn.apply(this, args)
					resolve(result)
				}, remainTime)
			}
		})
	}
	return _throttle
}
